// -*- Mode: objc; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-'
//
// This file is part of the cocoa-armory
// Copyright (C) 2007  alfonso e. urdaneta
//                     alfonso.e.urdaneta@gmail.com

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "HttpUtils.h"

@implementation HttpUtils


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [received_data setLength:0];
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [received_data appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"HttpUtils.m: didFailWithError");
    [connection release];
    [received_data release];
}

/**
 * called whtn the URL has finished downloading
 *
 * @param connection the connection it is bound to
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *temp = [NSString stringWithCString:[received_data bytes] 
                               length:[received_data length]];
    NSLog(temp);
    [connection release];
    [received_data release];
}


/**
 * sends off the request to the web server
 * 
 * @param request_url the url we want to fetch
 */
- (void)issueRequest:(NSString *)request_url
{
    NSURLRequest *url_request
        =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://armory.worldofwarcraft.com/#arena-ladder.xml?b=Stormstrike&ts=5"]
                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                       timeoutInterval:60.0];
 
    NSURLConnection *url_connection=[[NSURLConnection alloc] initWithRequest:url_request delegate:self];
    if (url_connection) 
    {
        received_data = [[NSMutableData data] retain];   
    } 
    else 
    {
        // inform the user that the download could not be made
    }
}
@end
